package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import edu.whu.demo.domain.Activity;
import edu.whu.demo.domain.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.whu.demo.domain.CommentDTO;
import edu.whu.demo.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@Mapper
public interface CommentDao extends BaseMapper<Comment> {

/*    @Select("select * from comment where id=#{id}")
    Comment findCommentById(long id);*/

    /**
     * 找到某一级评论下面的所有二级评论
     * @param id
     * @return
     */
    @Select("select * from comment p left join user u on p.from_uid=u.uid where type='comment' and parent_id=#{id}")
    @Results({
            @Result(property = "fromUid", column = "uid"),
            @Result(property = "fromName", column = "name")

    })
    List<CommentDTO> findSecondCommentsByParentId(long id);

    //找到所有的一级评论
    @Select("select * from comment p left join user u on p.from_uid=u.uid where type= 'dynamic' and parent_id=#{id}")
    @Results({
            @Result(property = "fromUid", column = "uid"),
            @Result(property = "fromName", column = "name")

    })
    List<CommentDTO> findFirstCommentsByParentId(long id);

    //点赞
    @Update("update comment set likes=likes+1 where id =#{id} ")
    void likeComments(Long id);

    @Update("update comment set likes=likes-1 where id =#{id} ")
    void disLikeComments(Long id);

    @Select("select id,content,likes,time,uid,name,picture from comment p left join user u on p.from_uid=u.uid "
            + "${ew.customSqlSegment}")
    @Results({
            @Result(property = "fromUid", column = "uid"),
            @Result(property = "fromName", column = "name")

    })
    IPage<CommentDTO> searchComment(IPage<CommentDTO> page, @Param(Constants.WRAPPER) QueryWrapper<CommentDTO> wrapper);

    @Select("select * from comment where send=0")
    List<Comment> getAll();
}